<?php

/**
 * This is the model class for table "tweets".
 *
 * The followings are the available columns in table 'tweets':
 * @property integer $id
 * @property string $content
 * @property integer $user_id
 * @property string $created_time
 * @property integer $parent_id
 *
 * The followings are the available model relations:
 * @property Favorites[] $favorites
 * @property Mentions[] $mentions
 * @property Timeline[] $timelines
 * @property Tweets $parent
 * @property Tweets[] $tweets
 * @property Users $user
 */
class Tweets extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Tweets the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tweets';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, created_time', 'required'),
			array('user_id, parent_id', 'numerical', 'integerOnly'=>true),
			array('content', 'length', 'max'=>500),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, content, user_id, created_time, parent_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'favorites' => array(self::HAS_MANY, 'Favorites', 'tweet_id'),
			'mentions' => array(self::HAS_MANY, 'Mentions', 'tweet_id'),
			'timelines' => array(self::HAS_MANY, 'Timeline', 'tweet_id'),
			'parent' => array(self::BELONGS_TO, 'Tweets', 'parent_id'),
			'tweets' => array(self::HAS_MANY, 'Tweets', 'parent_id'),
			'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'content' => 'Content',
			'user_id' => 'User',
			'created_time' => 'Created Time',
			'parent_id' => 'Parent',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('created_time',$this->created_time,true);
		$criteria->compare('parent_id',$this->parent_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        /**
         * Find Real ID of a tweet. Usefull when find a retweet id
         * @param int $tweed_id
         * @return parentID or NULL if not found tweet
         */
        public static function findRealTweetID($tweet_id, $objectOut = false)
        {
                /**
                 * @var Tweets $tweets
                 */
                $tweet = Tweets::model()->findByPk($tweet_id);
                if ($tweet){
                    if ($tweet->content){
                        if ($objectOut)
                            return $tweet;
                        return $tweet->id;
                    }
                    else{
                        if ($objectOut)
                            return $tweet->parent;
                        return $tweet->parent_id;
                    }
                }
                else
                    return null;
        }
        
        /**
         * Find the number of people who favorited this tweet
         * @param int $tweet_id ID of a Tweet
         * @return int the number of people who favorited this tweet
         */
        public static function countFavorites($tweet_id)
        {
            return Favorites::model()->countByAttributes(array('tweet_id'=>$tweet_id));
        }
        
        /**
         * Find the number of people who retweeted this tweet
         * @param int $tweet_id ID of a Tweet
         * @return int the number of people who retweeded this tweet
         */
        public static function countRetweets($tweet_id)
        {
            return Tweets::model()->countByAttributes(array(
                'parent_id'=>$tweet_id,
                'content'  =>null,
            ));
        }
        
        /**
         * Find the number of people who replyed this tweet
         * @param int $tweet_id ID of a Tweet
         * @return int the number of people who replyed this tweet
         */
        public static function countReplys($tweet_id)
        {
            return Tweets::model()->count("content is not null and parent_id=:parent_id",array(
                ':parent_id'=>$tweet_id,
            ));
        }
        
        
}